package mark.tom.fiter.demo;

import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.nio.charset.Charset;
import java.util.List;

import mark.tom.fiter.Buz;
import mark.tom.fiter.FastJsonDataResolver;
import mark.tom.fiter.GsonDataResolver;
import mark.tom.fiter.HttpRequestManager;
import mark.tom.fiter.RequestListener;
import mark.tom.fiter.ResolverFactory;
import mark.tom.fiter.ResponseResolver;
import mark.tom.fiter.bean.AggregateBean;
import mark.tom.fiter.bean.config.OkHttpConfig;
import mark.tom.fiter.bean.config.RequestConfig;
import mark.tom.fiter.demo.databinding.ActivityMainBinding;
import mark.tom.fiter.http.ssl.NoneSSLSocketFactory;
import mark.tom.fiter.json.BaseJSONObject;

import static mark.tom.fiter.bean.config.RequestConfig.RESPONSE_BYTE;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .permitAll()
                .build()
        );

        HttpRequestManager.initHttpConfig(new Buz.Builder()
                .okHttpConfig(new OkHttpConfig().setSslSocketFactory(NoneSSLSocketFactory.getHttpSSLSocketFactory()))
                .resolver(new ResolverFactory().get(new GsonDataResolver()))
                .build());

        String testJsonArrayStr = "[{\"accessToken\":\"adadasdasdasdas\"},{\"accessToken\":\"czczxczcsasdfasas\"}]";
        List<Object> result = new GsonDataResolver().parseArray(testJsonArrayStr, TestBO.class);
        Log.d("DataResolver", result.toString());

        ActivityMainBinding viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());
        viewBinding.test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 请求文件数据
                RequestConfig c = new RequestConfig.Builder()
                        .resType(RESPONSE_BYTE)
                        .build();
                HttpRequestManager.sendGetRequest(MainActivity.this,
                        "https://futuresiwinb.hsmdb.com/qihuo/cloud_common_config.xml", (BaseJSONObject) null, null, c, new RequestListener<byte[]>() {
                            @Override
                            public void onSuccess(byte[] mItem) {
                                super.onSuccess(mItem);
                                Log.d("GetRequest", new String(mItem, Charset.forName("UTF8")));
                            }
                        });


                BaseJSONObject header = new BaseJSONObject();
                header.put("testHeader", "test");

                BaseJSONObject param = new BaseJSONObject();
                param.put("secret", "953db157900a4d758c52d8035812875c");

                // 发送普通的Get请求  不进行数据处理
                HttpRequestManager.sendGetRequest(MainActivity.this,
                        "https://futurecloud.hsmdb.com/outer-quote/quote/access_token", param, header, null, new RequestListener<String>() {
                            @Override
                            public void onSuccess(String mItem) {
                                super.onSuccess(mItem);
                                Log.d("GetRequest", mItem);
                            }
                        });

                // 发送普通的Get请求  将返回数据解析成对应的DTO对象
                RequestConfig config = new RequestConfig.Builder()
                        .clz(TestDTO.class)
                        .build();
                HttpRequestManager.sendGetRequest(MainActivity.this,
                        "https://futurecloud.hsmdb.com/outer-quote/quote/access_token", param, header, config, new RequestListener<TestDTO>() {
                            @Override
                            public void onSuccess(TestDTO mItem) {
                                super.onSuccess(mItem);
                                Log.d("GetRequest", mItem.toString());
                            }
                        });

                // 发送普通的Get请求  将返回数据解析成对应的DTO对象 同时再转换为BO对象
                config = new RequestConfig.Builder()
                        .clz(TestDTO.class)
                        .converter(new TestBO.TestDTOConverter())
                        .build();
                HttpRequestManager.sendGetRequest(MainActivity.this,
                        "https://futurecloud.hsmdb.com/outer-quote/quote/access_token", param, header, config, new RequestListener<TestBO>() {
                            @Override
                            public void onSuccess(TestBO mItem) {
                                super.onSuccess(mItem);
                                Log.d("GetRequest", mItem.toString());
                            }
                        });

                // 发送普通的Post请求  不进行数据处理
                HttpRequestManager.sendPostRequest(MainActivity.this,
                        "https://futurecloud.hsmdb.com/outer-quote/quote/access_token", param, header, null, new RequestListener<String>() {
                            @Override
                            public void onSuccess(String mItem) {
                                super.onSuccess(mItem);
                                Log.d("PostRequest", mItem);
                            }
                        });

                // 发送普通的Post请求  将返回数据解析成对应的DTO对象
                config = new RequestConfig.Builder()
                        .clz(TestDTO.class)
                        .build();
                HttpRequestManager.sendPostRequest(MainActivity.this,
                        "https://futurecloud.hsmdb.com/outer-quote/quote/access_token", param, header, config, new RequestListener<TestDTO>() {
                            @Override
                            public void onSuccess(TestDTO mItem) {
                                super.onSuccess(mItem);
                                Log.d("PostRequest", mItem.toString());
                            }
                        });

                // 发送普通的Post请求  将返回数据解析成对应的DTO对象 同时再转换为BO对象
                config = new RequestConfig.Builder()
                        .clz(TestDTO.class)
                        .converter(new TestBO.TestDTOConverter())
                        .build();
                HttpRequestManager.sendPostRequest(MainActivity.this,
                        "https://futurecloud.hsmdb.com/outer-quote/quote/access_token", param, header, config, new RequestListener<TestBO>() {
                            @Override
                            public void onSuccess(TestBO mItem) {
                                super.onSuccess(mItem);
                                Log.d("PostRequest", mItem.toString());
                            }
                        });
            }
        });
    }
}
